package com.example.controller;

import com.example.dto.PayRequest;
import com.example.dto.PayResponse;
import com.example.service.SysPayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
public class SysPayController {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private SysPayService sysPayService;

    /***
     * 处理支付的回调
     * @param notifyData
     * @return
     */
    @PostMapping(value = "notify")
    public String notify(@RequestBody String notifyData) {
        return sysPayService.notify(notifyData);
    }

    /****
     * 用户发起支付申请
     * @param payRequest
     * @return
     */
    @PostMapping(value = "apply")
    public PayResponse apply(PayRequest payRequest, HttpServletRequest httpServletRequest) {
        try {
            return sysPayService.apply(payRequest, httpServletRequest);
        } catch (Exception e) {
            return PayResponse.error(e.getMessage());
        }
    }
}
